﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;
using HYView;

namespace HYRElectric.WirePlace
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CreateConduitByWire : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			this.doc = activeUIDocument.Document;
			if (!(cmdData.Application.ActiveUIDocument.Document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((HYView.CmdViewType)3, cmdData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			using (DlgCreateConduitConfig dlgCreateConduitConfig = new DlgCreateConduitConfig(this.doc))
			{
				dlgCreateConduitConfig.Show(WindowHelper.HWndRevit);
				WindowHelper.SendMessageToWndRevit();
				CreateConduitFunction createConduitFunction = new CreateConduitFunction(activeUIDocument);
				bool baseDatas = createConduitFunction.GetBaseDatas();
				while (baseDatas)
				{
					try
					{
						createConduitFunction.CmdCreateConduitByWire(activeUIDocument, dlgCreateConduitConfig);
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
						dlgCreateConduitConfig.Close();
						break;
					}
					catch (Exception)
					{
						TaskDialog.Show("盈建科提示", "导线生成线管失败。");
						dlgCreateConduitConfig.Close();
						break;
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private List<Solid> GetElementSolids(Element element)
		{
			Options options = this.m_UIApp.Application.Create.NewGeometryOptions();
            options.DetailLevel = (ViewDetailLevel)3;
			options.IncludeNonVisibleObjects = true;
			GeometryElement geometryElement;
			if (element.GetType() == typeof(FamilyInstance))
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				geometryElement = familyInstance.GetOriginalGeometry(options);
				geometryElement = geometryElement.GetTransformed(familyInstance.GetTransform());
			}
			else
			{
				geometryElement = element.get_Geometry(options);
			}
			List<Solid> list = new List<Solid>();
			if (null == geometryElement)
			{
				return list;
			}
			foreach (GeometryObject geometryObject in geometryElement)
			{
				if (!(geometryObject.GetType() != typeof(Solid)))
				{
					Solid solid = geometryObject as Solid;
					if (solid != null)
					{
						list.Add(solid);
					}
				}
			}
			return list;
		}

		private List<Floor> SelectWire(UIDocument uiDoc)
		{
			List<Floor> result = new List<Floor>();
			List<Reference> list = uiDoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new SelectionFilterFloor(), "请选择要生成线管的导线").ToList<Reference>();
			if (list.Count == 0)
			{
				TaskDialog.Show("盈建科提示", "未选中导线，请重新选择。");
				return result;
			}
			return (from pickObj in list
			select uiDoc.Document.GetElement(pickObj.ElementId) as Floor).ToList<Floor>();
		}

		private Document doc;

		public bool isError;

		private UIApplication m_UIApp;
	}
}
